#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include <graphics.h>
#include <math.h>

#pragma comment(lib,"winmm.lib")
#define PI 3.1415926
#define High 764
#define Width 1019
#define danjia 10
#define EnemyNumber 4							//敌人数量
#define RopeNumber 6							//绳结数量
#define BarNumber 5				
#define ballnumber 5							//子弹数目
#define xianzhi 10								//子弹碰撞次数的限制

double u;										//阻力系数
double g;										//下落系数

//全局变量
struct people//射击者
{
	IMAGE photo;
	int x;//左上角坐标
	int y;
};
struct enemy//敌人参数
{
	IMAGE photo1;//图片
	IMAGE photo2;
	int x;
	int y;
	int H;//图片高度
	int W;//图片宽度
	int left;//左边界
	int right;//右边界
	int top;//上边界
	int bottom;//下边界
	int live;//敌人存活的判定
};
struct bullet					//子弹参数
{
	IMAGE photo1;
	IMAGE photo2;
	double x;					//坐标
	double y;
	double v_x;					//速度
	double v_y;
	int r;
	int carshnumber;			//碰撞次数
	int number;
	int appearance;

};
typedef struct _bar				//障碍物参数
{
	IMAGE photo;
	int x;						//左上角坐标
	int y;
	int H;
	int W;
	int left;
	int right;
	int top;
	int bottom;
}jam;

struct bar
{
	double vx,vy;					//纸箱的初速度
	double x,y,H,W;					//纸箱的坐标
	double top,bottom,left,right;	//纸箱的上下左右
	int live;						//纸箱被击中的判定
}trunk;
struct photo						//灵魂
{
	IMAGE p1;
	IMAGE p2;
	double vx,vy;
	double x,y;
}c[2];
struct _rope //绳子的结构体
{
	IMAGE photo;
	int x;
	int y;
};
struct bar1						//炸药
{
	IMAGE p1;						//炸药图片
	IMAGE p2;
	double x,y,H,W;					//炸药的坐标
	double top,bottom,left,right;	//炸药的上下左右
	int live;
}EM;

int bullet_number;//子弹总数
int i;//循环变量
int enemy_number1 = EnemyNumber;//第一关敌人剩余数
int enemy_number2 = EnemyNumber;//第二关敌人剩余数
int enemy_number3 = EnemyNumber;//第三关敌人剩余数
int GameState = 0;//游戏状态，0表示开始界面，1表示第一关，2表示第二关，3表示第三关，4表示结束界面。
int NodeNumber;				//绳子的断裂点


double TanValue;//子弹角度的tan值
double radian;//弧度

IMAGE background;				//游戏背景
IMAGE start;					//开始界面的背景
IMAGE over;						//结束页面图片
IMAGE bar_trunk;                //木箱子图片
IMAGE dphoto1_1;				//死亡图片
IMAGE dphoto1_2;
IMAGE dphoto2_1;					
IMAGE dphoto2_2;
IMAGE pausephoto;					//暂停页面
IMAGE vicyory;					//胜利页面

_rope rope[RopeNumber];					//定义绳结
enemy a[EnemyNumber];			//定义多个敌人
bullet ball;					//定义一个子弹
people shoter;					//定义射击者
jam b[BarNumber];				//定义多个障碍物
bullet IronBall;				//定义绳子悬挂的铁球
MOUSEMSG m;


void startup4()//结束页面
{
	//背景音乐
	mciSendString("open backmusic.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat",NULL,0,NULL);
	initgraph(Width, High);
	loadimage(&over,"over.png");
	loadimage(&vicyory,"victoryend.png");
	loadimage(&pausephoto,"pause.png");
	BeginBatchDraw();
}
void startup1() // 第一关数据初始化
{	
	//背景音乐
	mciSendString("open backmusic.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat",NULL,0,NULL);
	initgraph(Width, High);
	loadimage(&background,"背景.jpg");				//载入图片
	loadimage(&pausephoto,"pause.png");
	loadimage(&over,"over.png");
	loadimage(&start,"start.png");
	loadimage(&vicyory,"victoryend.png");
	loadimage(&b[0].photo,"砖块1.png");
	loadimage(&b[1].photo,"砖块2.png");
	loadimage(&b[2].photo,"砖块3.png");
	loadimage(&b[3].photo,"砖块4.png");
	loadimage(&b[4].photo,"砖块1.png");

	loadimage(&a[0].photo1,"敌人1.png");
	loadimage(&a[0].photo2,"敌人2.png");
	loadimage(&a[1].photo1,"敌人1.png");
	loadimage(&a[1].photo2,"敌人2.png");
	loadimage(&a[2].photo1,"敌人1.png");
	loadimage(&a[2].photo2,"敌人2.png");
	loadimage(&a[3].photo1,"敌人1.png");
	loadimage(&a[3].photo2,"敌人2.png");

	loadimage(&c[0].p1,"soul1_1.png");
	loadimage(&c[0].p2,"soul1_2.png");
	loadimage(&c[1].p1,"soul2_1.png");
	loadimage(&c[1].p2,"soul2_2.png");
	

	loadimage(&bar_trunk,"木箱子.jpg");
	loadimage(&EM.p1,"炸药1.png");
	loadimage(&EM.p2,"炸药2.png");
	
	loadimage(&ball.photo1,"雪球1.png");
	loadimage(&ball.photo2,"雪球2.png");
	loadimage(&IronBall.photo1,"Ironball1.png");
	loadimage(&IronBall.photo2,"Ironball2.png");
	
	for(i=0;i<RopeNumber;i++)//绳结
	{
		loadimage(&rope[i].photo,"rope.png");
		rope[i].x=-100;
		rope[i].y=i*52;
	}
	NodeNumber=10;
	IronBall.r=50;//铁球
	IronBall.x=-200;
	IronBall.y=rope[5].y+52;
	IronBall.v_y=0;
	shoter.x = Width / 10;							//射击者位置
	shoter.y = High * 9 / 10;
	ball.x = shoter.x;
	ball.y = shoter.y;
	ball.v_y = 0;
	ball.v_x = 0;
	ball.r = 15;
	ball.carshnumber = 0;
	ball.number = 0;
	ball.appearance=1;
	trunk.vx=0,trunk.vy=0;
	trunk.live=0;
	EM.live=1;
	u=0;
	g=5;

	b[0].x = 769;					//障碍
	b[0].y = 450;
	b[0].H = 32;
	b[0].W = 250;
	b[0].bottom = b[0].y + b[0].H;
	b[0].left = b[0].x;
	b[0].right = b[0].x + b[0].W;
	b[0].top = b[0].y;
	b[1].x = 750;
	b[1].y = 150;
	b[1].H = 30;
	b[1].W = 60;
	b[1].bottom = b[1].y + b[1].H;
	b[1].left = b[1].x;
	b[1].right = b[1].x + b[1].W;
	b[1].top = b[1].y;
	b[2].x = 450;					
	b[2].y = 150;
	b[2].H = 33;
	b[2].W = 100;
	b[2].bottom = b[2].y + b[2].H;
	b[2].left = b[2].x;
	b[2].right = b[2].x + b[2].W;
	b[2].top = b[2].y;
	b[3].H = 100;
	b[3].W = 33;
	b[3].x = b[0].x-b[3].W;					
	b[3].y = b[0].y+b[0].H-b[3].H;
	b[3].bottom = b[3].y + b[3].H;
	b[3].left = b[3].x;
	b[3].right = b[3].x + b[3].W;
	b[3].top = b[3].y;
	b[4].x = 0;					
	b[4].y = 304;
	b[4].H = 32;
	b[4].W = 250;
	b[4].bottom = b[4].y + b[4].H;
	b[4].left = b[4].x;
	b[4].right = b[4].x + b[4].W;
	b[4].top = b[4].y;

	trunk.x = 190;					
	trunk.y = 247;
	trunk.H = 57;
	trunk.W = 60;
	trunk.bottom = trunk.y + trunk.H;
	trunk.left = trunk.x;
	trunk.right = trunk.x + trunk.W;
	trunk.top = trunk.y;

	EM.x = 800;					
	EM.y = 480;
	EM.H = 40;
	EM.W = 40;
	EM.bottom = EM.y + EM.H;
	EM.left = EM.x;
	EM.right = EM.x + EM.W;
	EM.top = EM.y;


	a[0].H = 20;
	a[0].W = 50;
	a[0].x = b[0].x+20;//敌人
	a[0].y = b[0].y-5.2*a[0].H;
	a[0].live = 1;
	a[0].bottom = a[0].y - a[0].H;
	a[0].top = a[0].y;
	a[0].left = a[0].x;
	a[0].right = a[0].x + a[0].W;
	a[1].H = 20;
	a[1].W = 50;
	a[1].x = b[1].x-20;//敌人
	a[1].y = b[1].y-5.2*a[1].H;
	a[1].live = 1;
	a[1].bottom = a[1].y - a[1].H;
	a[1].top = a[1].y;
	a[1].left = a[1].x;
	a[1].right = a[1].x + a[1].W;
	a[2].H = 20;
	a[2].W = 50;
	a[2].x = b[2].x;//敌人
	a[2].y = b[2].y-5.2*a[2].H;
	a[2].live = 1;
	a[2].bottom = a[2].y - a[2].H;
	a[2].top = a[2].y;
	a[2].left = a[2].x;
	a[2].right = a[2].x + a[2].W;
	a[3].H = 20;
	a[3].W = 50;
	a[3].x = b[4].x+2*20;//敌人
	a[3].y = b[4].y-5.2*a[3].H;
	a[3].live = 1;
	a[3].bottom = a[3].y - a[3].H;
	a[3].top = a[3].y;
	a[3].left = a[3].x;
	a[3].right = a[3].x + a[3].W;
	BeginBatchDraw();
}
void startup0()
{
	//背景音乐
	mciSendString("open backmusic.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat",NULL,0,NULL);
	initgraph(Width, High);
	loadimage(&start,"start.png");
	loadimage(&vicyory,"victoryend.png");
	loadimage(&pausephoto,"pause.png");
	BeginBatchDraw();
}
void startup2()
{
	//背景音乐
	mciSendString("open backmusic.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat",NULL,0,NULL);
	initgraph(Width, High);
	loadimage(&background,"背景.jpg");				//载入图片
	loadimage(&pausephoto,"pause.png");
	loadimage(&start,"start.png");
	loadimage(&over,"over.png");
	loadimage(&vicyory,"victoryend.png");
	loadimage(&b[0].photo,"砖块1.png");
	loadimage(&b[1].photo,"砖块2.png");
	loadimage(&b[2].photo,"砖块4.png");
	loadimage(&b[3].photo,"砖块4.png");
	loadimage(&b[4].photo,"砖块1.png");

	loadimage(&a[0].photo1,"敌人1.png");
	loadimage(&a[0].photo2,"敌人2.png");
	loadimage(&a[1].photo1,"敌人1.png");
	loadimage(&a[1].photo2,"敌人2.png");
	loadimage(&a[2].photo1,"敌人1.png");
	loadimage(&a[2].photo2,"敌人2.png");
	loadimage(&a[3].photo1,"敌人1.png");
	loadimage(&a[3].photo2,"敌人2.png");

	loadimage(&c[0].p1,"soul1_1.png");
	loadimage(&c[0].p2,"soul1_2.png");
	loadimage(&c[1].p1,"soul2_1.png");
	loadimage(&c[1].p2,"soul2_2.png");
	

	
	loadimage(&EM.p1,"炸药1.png");
	loadimage(&EM.p2,"炸药2.png");
	
	loadimage(&ball.photo1,"雪球1.png");
	loadimage(&ball.photo2,"雪球2.png");
	loadimage(&IronBall.photo1,"Ironball1.png");
	loadimage(&IronBall.photo2,"Ironball2.png");
	
	for(i=0;i<RopeNumber;i++)//绳结
	{
		loadimage(&rope[i].photo,"rope.png");
		rope[i].x=800;
		rope[i].y=-52*3+i*52;
	}
	IronBall.r=50;//铁球
	IronBall.x=800-IronBall.r;
	IronBall.y=rope[4].y+52;
	IronBall.v_y=0;
	NodeNumber=10;
	shoter.x = Width / 10;							//射击者位置
	shoter.y = High * 9 / 10;
	ball.x = shoter.x;
	ball.y = shoter.y;
	ball.v_y = 0;
	ball.v_x = 0;
	ball.r = 15;
	ball.carshnumber = 0;
	ball.number = 0;
	ball.appearance=1;
	trunk.vx=0,trunk.vy=0;
	trunk.live=0;
	EM.live=1;
	u=0;
	g=0.1;

	b[0].x = 0;					//障碍
	b[0].y = 200;
	b[0].H = 32;
	b[0].W = 250;
	b[0].bottom = b[0].y + b[0].H;
	b[0].left = b[0].x;
	b[0].right = b[0].x + b[0].W;
	b[0].top = b[0].y;
	b[1].x = 750;
	b[1].y = 300;
	b[1].H = 30;
	b[1].W = 60;
	b[1].bottom = b[1].y + b[1].H;
	b[1].left = b[1].x;
	b[1].right = b[1].x + b[1].W;
	b[1].top = b[1].y;
	
	b[2].x=250-33;;
	b[2].y=200-99;;
	b[2].H = 33;
	b[2].W = 100;
	b[2].bottom = b[2].y + b[2].H;
	b[2].left = b[2].x;
	b[2].right = b[2].x + b[2].W;
	b[2].top = b[2].y;
	b[3].H = 100;
	b[3].W = 33;
	b[3].x = 800;					
	b[3].y = 663;
	b[3].bottom = b[3].y + b[3].H;
	b[3].left = b[3].x;
	b[3].right = b[3].x + b[3].W;
	b[3].top = b[3].y;
	
	b[4].x=700;
	b[4].y=300;
	b[4].H = 32;
	b[4].W = 250;
	b[4].bottom = b[4].y + b[4].H;
	b[4].left = b[4].x;
	b[4].right = b[4].x + b[4].W;
	b[4].top = b[4].y;

	
	EM.x=750;
	EM.y=723;
	EM.H = 40;
	EM.W = 40;
	EM.bottom = EM.y + EM.H;
	EM.left = EM.x;
	EM.right = EM.x + EM.W;
	EM.top = EM.y;


	a[0].H = 20;
	a[0].W = 50;
	a[0].x = b[0].x+10;//敌人
	a[0].y = b[0].y-5.2*a[0].H;
	a[0].live = 1;
	a[0].bottom = a[0].y - a[0].H;
	a[0].top = a[0].y;
	a[0].left = a[0].x;
	a[0].right = a[0].x + a[0].W;
	a[1].H = 20;
	a[1].W = 50;
	a[1].x = b[0].x+90;//敌人
	a[1].y = b[0].y-5.2*a[0].H;
	a[1].live = 1;
	a[1].bottom = a[1].y - a[1].H;
	a[1].top = a[1].y;
	a[1].left = a[1].x;
	a[1].right = a[1].x + a[1].W;
	a[2].H = 20;
	a[2].W = 50;
	a[2].x = b[3].x+30;//敌人
	a[2].y = 760-5.2*a[2].H;
	a[2].live = 1;
	a[2].bottom = a[2].y - a[2].H;
	a[2].top = a[2].y;
	a[2].left = a[2].x;
	a[2].right = a[2].x + a[2].W;
	a[3].H = 20;
	a[3].W = 50;
	a[3].x = b[1].x+20;//敌人
	a[3].y = b[1].y-5.2*a[3].H;
	a[3].live = 1;
	a[3].bottom = a[3].y - a[3].H;
	a[3].top = a[3].y;
	a[3].left = a[3].x;
	a[3].right = a[3].x + a[3].W;
	BeginBatchDraw();
}
void startup3()
{
	//背景音乐
	mciSendString("open backmusic.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat",NULL,0,NULL);
	initgraph(Width, High);
	loadimage(&background,"背景.jpg");				//载入图片
	loadimage(&pausephoto,"pause.png");
	loadimage(&start,"start.png");
	loadimage(&vicyory,"victoryend.png");
	loadimage(&b[0].photo,"砖块1.png");
	loadimage(&b[1].photo,"砖块4.png");
	loadimage(&b[2].photo,"砖块3.png");
	loadimage(&b[3].photo,"砖块4.png");
	loadimage(&b[4].photo,"砖块1.png");

	loadimage(&a[0].photo1,"敌人1.png");
	loadimage(&a[0].photo2,"敌人2.png");
	loadimage(&a[1].photo1,"敌人1.png");
	loadimage(&a[1].photo2,"敌人2.png");
	loadimage(&a[2].photo1,"敌人1.png");
	loadimage(&a[2].photo2,"敌人2.png");
	loadimage(&a[3].photo1,"敌人1.png");
	loadimage(&a[3].photo2,"敌人2.png"); 
	loadimage(&c[0].p1,"soul1_1.png");
	loadimage(&c[0].p2,"soul1_2.png");
	loadimage(&c[1].p1,"soul2_1.png");
	loadimage(&c[1].p2,"soul2_2.png");
	loadimage(&EM.p1,"炸药1.png");
	loadimage(&EM.p2,"炸药2.png");
	loadimage(&ball.photo1,"雪球1.png");
	loadimage(&ball.photo2,"雪球2.png");
	loadimage(&IronBall.photo1,"Ironball1.png");
	loadimage(&IronBall.photo2,"Ironball2.png");
	
	for(i=0;i<RopeNumber;i++)//绳结
	{
		loadimage(&rope[i].photo,"rope.png");
		rope[i].x=900;
		rope[i].y=i*52;
	}
	NodeNumber=10;
	IronBall.r=50;//铁球
	IronBall.x=900-IronBall.r;
	IronBall.y=rope[5].y+52;
	IronBall.v_y=0;
	shoter.x = Width / 10;							//射击者位置
	shoter.y = High * 9 / 10;
	ball.x = shoter.x;
	ball.y = shoter.y;
	ball.v_y = 0;
	ball.v_x = 0;
	ball.r = 15;
	ball.carshnumber = 0;
	ball.number = 0;
	ball.appearance=1;
	EM.live=1;
	u=0;
	g=5;

	b[0].x = 450;					//障碍
	b[0].y = 400;
	b[0].H = 32;
	b[0].W = 250;
	b[0].bottom = b[0].y + b[0].H;
	b[0].left = b[0].x;
	b[0].right = b[0].x + b[0].W;
	b[0].top = b[0].y;

	b[1].x = 800;
	b[1].y = 664;
	b[1].H = 100;
	b[1].W = 33;
	b[1].bottom = b[1].y + b[1].H;
	b[1].left = b[1].x;
	b[1].right = b[1].x + b[1].W;
	b[1].top = b[1].y;

	b[2].x = 450;					
	b[2].y = 632;
	b[2].H = 33;
	b[2].W = 100;
	b[2].bottom = b[2].y + b[2].H;
	b[2].left = b[2].x;
	b[2].right = b[2].x + b[2].W;
	b[2].top = b[2].y;

	b[3].x = 450;					
	b[3].y = 664;
	b[3].H = 100;
	b[3].W = 33;
	b[3].bottom = b[3].y + b[3].H;
	b[3].left = b[3].x;
	b[3].right = b[3].x + b[3].W;
	b[3].top = b[3].y;
	b[4].x = 300;					
	b[4].y = 100;
	b[4].H = 32;
	b[4].W = 250;
	b[4].bottom = b[4].y + b[4].H;
	b[4].left = b[4].x;
	b[4].right = b[4].x + b[4].W;
	b[4].top = b[4].y;

	EM.x = 360;					
	EM.y = 130;
	EM.H = 40;
	EM.W = 40;
	EM.bottom = EM.y + EM.H;
	EM.left = EM.x;
	EM.right = EM.x + EM.W;
	EM.top = EM.y;

	a[0].H = 20;
	a[0].W = 50;
	a[0].x = b[0].x+60;//敌人
	a[0].y = b[0].y-5.2*a[0].H;
	a[0].live = 1;
	a[0].bottom = a[0].y - a[0].H;
	a[0].top = a[0].y;
	a[0].left = a[0].x;
	a[0].right = a[0].x + a[0].W;
	a[1].H = 20;
	a[1].W = 50;
	a[1].x = 860;//敌人
	a[1].y = 659;
	a[1].live = 1;
	a[1].bottom = a[1].y - a[1].H;
	a[1].top = a[1].y;
	a[1].left = a[1].x;
	a[1].right = a[1].x + a[1].W;
	a[2].H = 20;
	a[2].W = 50;
	a[2].x = 600;//敌人
	a[2].y = 659;
	a[2].live = 1;
	a[2].bottom = a[2].y - a[2].H;
	a[2].top = a[2].y;
	a[2].left = a[2].x;
	a[2].right = a[2].x + a[2].W;
	a[3].H = 20;
	a[3].W = 50;
	a[3].x = b[4].x+2*20;//敌人
	a[3].y = b[4].y-5.2*a[3].H;
	a[3].live = 1;
	a[3].bottom = a[3].y - a[3].H;
	a[3].top = a[3].y;
	a[3].left = a[3].x;
	a[3].right = a[3].x + a[3].W;
	BeginBatchDraw();
}
void startup()
{
	enemy_number1 = EnemyNumber;//第一关敌人剩余数
    enemy_number2 = EnemyNumber;//第二关敌人剩余数
    enemy_number3 = EnemyNumber;//第三关敌人剩余数
	switch (GameState)
	{
	case 0:
		startup0();
		break;
	case 1:
		startup1();
		break;
	case 2:
		startup2();
		break;
	case 3:
		startup3();
		break;
	case 4:
		startup4();
		break;
	default :
		break;
	}
}
void readRecordFile()			//读档
{
	FILE *fp;
	fp=fopen(".\\gameRecord.dat","r");
	fscanf(fp,"%d",&GameState);
	fclose(fp);
}
void writeRecordFile()			//存档
{
	FILE *fp;
	fp=fopen(".\\gameRecord.dat","w");
	fprintf(fp,"%d",GameState);
	fclose(fp);
}
void GameOver()
{
	EndBatchDraw();
	getch();
	closegraph();
}
void GameShow0()//显示
{
	cleardevice();
	putimage(0,0,&start);
	FlushBatchDraw();
}
void GameShow1()
{
	cleardevice();
	if(enemy_number1==0 && GameState==1)
	{
		GameState=2;
		startup();
	}
	else if(enemy_number2==0 && GameState==2)
	{
		GameState=3;
		startup();
	}
	else if((enemy_number1!=0 && ball.number == ballnumber)||(enemy_number2!=0 && ball.number == ballnumber)||(enemy_number3!=0 && ball.number == ballnumber))
	{
		GameState=4;
		startup();
	}
	else if(enemy_number3==0 && GameState==3)
	{
		putimage(0,0,&vicyory);
		FlushBatchDraw();
		while (1)
		{
			char input3;
		if(kbhit())
		{
				input3=getch();
				if (input3=='e'||input3=='E')
				{
					GameState=0;
					startup();
					break;
				}
		}

		}
	}
	putimage(0,0,&background);				//背景图片
	putimage(trunk.x,trunk.y,&bar_trunk);	//木箱子图片
	//显示炸药的判断
	if(EM.live == 1)
	{
		putimage(EM.x,EM.y,&EM.p1);				//炸药图片
		putimage(EM.x,EM.y,&EM.p2);
	}
	//子弹显示的判断
	if (ball.carshnumber <= xianzhi && ball.number <= ballnumber)
	{
		setfillcolor(GREEN);
		putimage(ball.x-ball.r,ball.y-ball.r,&ball.photo2,NOTSRCERASE);
		putimage(ball.x-ball.r,ball.y-ball.r,&ball.photo1,SRCINVERT);
	}
	else if(ball.carshnumber>xianzhi && ball.number < ballnumber)
	{
		ball.carshnumber=0;
		ball.x=shoter.x;
		ball.y=shoter.y;
		ball.v_y = 0;
		ball.v_x = 0;
		
		setfillcolor(GREEN);
		putimage(ball.x-ball.r,ball.y-ball.r,&ball.photo2,NOTSRCERASE);
		putimage(ball.x-ball.r,ball.y-ball.r,&ball.photo1,SRCINVERT);
	}
	//显示敌人的判断
	for (i = 0; i < EnemyNumber; i++)
	{
		if (a[i].live == 0)
		{
			c[0].x=a[i].x;
			c[0].y=a[i].y;
			putimage(c[0].x,c[0].y,&c[0].p2,NOTSRCERASE);
			putimage(c[0].x,c[0].y,&c[0].p1,SRCINVERT);	
			c[0].y--;
		}
		else
		{
			putimage(a[i].x,a[i].y,&a[i].photo2,NOTSRCERASE);
			putimage(a[i].x,a[i].y,&a[i].photo1,SRCINVERT);
		}
	}
	//显示障碍物的判断
	for (i = 0; i < BarNumber; i++)
	{
		putimage(b[i].x,b[i].y,&b[i].photo);
	}
	for(i=0;i<RopeNumber;i++)//显示绳子
	{
		putimage(rope[i].x,rope[i].y,&rope[i].photo);
	}
	putimage(IronBall.x,IronBall.y,&IronBall.photo2,NOTSRCERASE);
	putimage(IronBall.x,IronBall.y,&IronBall.photo1,SRCINVERT);
	//显示雪球剩余量
	for(i=0;i<BarNumber-ball.number;i++)
	{
		putimage(0+40*i,0,&ball.photo2,NOTSRCERASE);
		putimage(0+40*i,0,&ball.photo1,SRCINVERT);

	}
	Sleep(8);
	FlushBatchDraw();
}
void GameShow4()
{
	cleardevice();
	putimage(0,0,&over);
	FlushBatchDraw();
}
void show()  // 显示画面
{
	switch (GameState)
	{
	case 0:
		GameShow0();
		break;
	case 1:
		GameShow1();
		break;
	case 2:
		GameShow1();
		break;
	case 3:
		GameShow1();
		break;
	case 4:
		GameShow4();
		break;
	default :
		break;
	}
}

void updateWithoutInput()  // 与用户输入无关的更新
{
	
	//子弹位置更新
	ball.x = ball.v_x + ball.x;
	ball.y = ball.v_y + ball.y;
	//子弹碰撞
	if (ball.x - ball.r <= 0 || ball.x + ball.r >= Width)//撞到边界
	{
		ball.v_x = (-1) * ball.v_x;
		ball.carshnumber++;
	}
	else if (ball.y - ball.r <= 0 || ball.y + ball.r >= High)
	{
		ball.v_y = (-1) * ball.v_y;
		ball.carshnumber++;
	}
	for (i = 0; i < BarNumber; i++)
	{

		if ((abs(ball.x + ball.r - b[i].left)<=10|| abs(ball.x - ball.r - b[i].right)<=10) && (ball.y >= b[i].top && ball.y <= b[i].bottom))//撞到砖块
		{
			ball.v_x = (-1)*ball.v_x;
			ball.carshnumber++;
		}
		if ((abs(ball.y + ball.r - b[i].top) <= 10 || abs(ball.y - ball.r - b[i].bottom) <= 10) && (ball.x <= b[i].right && ball.x >= b[i].left))
		{
			ball.v_y = (-1)*ball.v_y;
			ball.carshnumber++;
		}
	}
	//子弹击中敌人
	for (i = 0; i < EnemyNumber; i++)
	{
		if ((abs(ball.x- a[i].left)<=ball.r+50|| abs(ball.x  - a[i].right)<=ball.r+50) && (abs(ball.y-a[i].top)<=ball.r && abs(ball.y-a[i].bottom)<=ball.r))
		{
			//子弹击中敌人的音效
			if(a[i].live==1)
			{	
				mciSendString("close kill",NULL,0,NULL);
				mciSendString("open hit.mp3 alias kill",NULL,0,NULL);
				mciSendString("play kill",NULL,0,NULL);
				if(GameState==1)
					enemy_number1--;
				else if(GameState==2)
					enemy_number2--;
				else if(GameState==3)
					enemy_number3--;
			}
			a[i].live = 0;
		}
		else if ((abs(ball.y - a[i].top) <= ball.r+50|| abs(ball.y - a[i].bottom) <= ball.r+50) && (abs(ball.x-a[i].right)<=ball.r && abs(ball.x-a[i].left)<=ball.r))
		{
			//子弹击中敌人的音效
			if(a[i].live==1)
			{	
				mciSendString("close kill",NULL,0,NULL);
				mciSendString("open hit.mp3 alias kill",NULL,0,NULL);
				mciSendString("play kill",NULL,0,NULL);
				if(GameState==1)
					enemy_number1--;
				else if(GameState==2)
					enemy_number2--;
				else if(GameState==3)
					enemy_number3--;
			}
			a[i].live = 0;
		}
	}
	//纸箱的移动
	if(abs(ball.x-trunk.left )<=ball.r+20 && abs(ball.y-trunk.top)<=ball.r && ball.y-trunk.bottom<=ball.r)
	{
		trunk.live=1;
		ball.appearance=0;
		trunk.vx=ball.v_x;
	}
	else if(abs(ball.x-trunk.right )<=ball.r && abs(ball.y-trunk.top)<=ball.r && ball.y-trunk.bottom<=ball.r)
	{
		trunk.live=1;
		ball.appearance=0;
		trunk.vx=ball.v_x;
	}

	if(trunk.live==1)
	{
		trunk.vx=trunk.vx-u;
	}
	trunk.x=trunk.x+trunk.vx;	
	trunk.y=trunk.y+trunk.vy;
	

	if(trunk.x>=b[i].right||trunk.x<=b[i].left)//纸箱掉下砖块
	{
		trunk.vy+=g;
	}
	if(trunk.bottom==High)
		trunk.y=0;
	if(trunk.vx==0)
		trunk.vx=0;
	//子弹击中炸药
	if((abs(ball.x- EM.left)<=ball.r+10|| abs(ball.x  - EM.right)<=ball.r+10) && (abs(ball.y-EM.top)<=ball.r+10&& abs(ball.y-EM.bottom)<=ball.r+10))
	{
		//子弹击中炸药的音效
		if(EM.live==1)
		{
			mciSendString("close bom",NULL,0,NULL);
			mciSendString("open boom.mp3 alias bom",NULL,0,NULL);
			mciSendString("play bom",NULL,0,NULL);
		}
		EM.live = 0;
		ball.carshnumber++;
		for(i=0;i<EnemyNumber;i++)
		{
			if(abs(a[i].x-EM.x)<=100 && abs(a[i].y-EM.y)<=200)
			{
				if(a[i].live==1)
				{
					if(GameState==1)
						enemy_number1--;
					else if(GameState==2)
						enemy_number2--;
					else if(GameState==3)
						enemy_number3--;
				}
				a[i].live=0;
			}
		}
	}
	else if((abs(ball.y - EM.top) <= ball.r+10|| abs(ball.y - EM.bottom) <= ball.r+10) && (abs(ball.x-EM.right)<=ball.r+10 && abs(ball.x-EM.left)<=ball.r+10))
	{
		//子弹击中炸药的音效
		if(EM.live==1)
		{
			mciSendString("close bom",NULL,0,NULL);
			mciSendString("open boom.mp3 alias bom",NULL,0,NULL);
			mciSendString("play bom",NULL,0,NULL);
		}
		EM.live = 0;
		ball.carshnumber++;
		for(i=0;i<EnemyNumber;i++)
		{
			if(abs(a[i].x-EM.x)<=100&& abs(a[i].y-EM.y)<=200)
			{
				if(a[i].live==1)
				{
					if(GameState==1)
						enemy_number1--;
					else if(GameState==2)
						enemy_number2--;
					else if(GameState==3)
						enemy_number3--;
				}
				a[i].live=0;
			}
		}
	}
	//子弹击中绳子
	if(abs(ball.x-rope[i].x)<10&&ball.y<RopeNumber*52&&(NodeNumber<0||NodeNumber>RopeNumber))
	{
		NodeNumber=ball.y/52;
		IronBall.v_y=g;
	}
	//绳子和铁球下坠
	for(i=NodeNumber;i<RopeNumber;i++)
	{
		IronBall.v_y+=g/2;
		rope[i].y+=IronBall.v_y;
	}
	if(IronBall.y+100<High&&NodeNumber!=10)
	{
		IronBall.y+=IronBall.v_y;
	}
	for(i=0;i<EnemyNumber;i++)//铁球击中敌人
	{
		if(abs(IronBall.y-a[i].y)<=IronBall.v_y && abs(IronBall.x-a[i].x)<=100)
		{
			if(a[i].live==1)
			{
				mciSendString("close kill",NULL,0,NULL);//击中敌人的音效
				mciSendString("open hit.mp3 alias kill",NULL,0,NULL);
				mciSendString("play kill",NULL,0,NULL);
				if(GameState==1)
					enemy_number1--;
				else if(GameState==2)
					enemy_number2--;
				else if(GameState==3)
					enemy_number3--;
			}
			a[i].live=0;
		}
	}

}
void updateWithInput()  // 与用户输入有关的更新
{
	char input;
	if(kbhit())
	{
		input=getch();
		if(input==27)
		while(1)
		{

			cleardevice();
			putimage(0,0,&pausephoto);
			char input2;
			if(kbhit())
			{
				input2=getch();
			if(input2=='G'||input2=='g')
			{
				writeRecordFile();
				break;
			}
			else if(input2=='E'||input2=='e')
			{
				GameState=0;
				startup();
				break;
			}
			}
				FlushBatchDraw();
		}
		
	}
	if (MouseHit()&&GameState!=0&&GameState!=4)//检测是否有鼠标消息
	{
		m = GetMouseMsg();
		if (m.uMsg == WM_MOUSEMOVE)
		{
			TanValue = (m.y - shoter.y) * 1.0 / (m.x - shoter.x);
		}
		else if (m.uMsg == WM_LBUTTONDOWN)
		{
			mciSendString("close f_gun",NULL,0,NULL);
			mciSendString("open shot.mp3 alias f_gun",NULL,0,NULL);
			mciSendString("play f_gun",NULL,0,NULL);
			ball.v_x = 10;
			ball.v_y = 10* TanValue;
			ball.number++;
		}
	}
	else if (MouseHit()&&GameState==0)//检测是否有鼠标消息
	{
		m = GetMouseMsg();
		if (m.uMsg == WM_LBUTTONDOWN)
		{
			if(m.x>185&&m.x<285&&m.y>225&&m.y<325)
			{
				GameState=1;
				startup();
			}
			else if(m.x>445&&m.x<555&&m.y>225&&m.y<325)
			{
				GameState=2;
				startup();
			}
			else if(m.x>715&&m.x<820&&m.y>225&&m.y<325)
			{
				GameState=3;
				startup();
			}
			else if(m.x>285&&m.x<715&&m.y>455&&m.y<700)
			{
				readRecordFile();
				startup();
				readRecordFile();
			}
		}
	}
	else if(MouseHit()&&GameState==4)
	{
		m = GetMouseMsg();
		if (m.uMsg == WM_LBUTTONDOWN)
		{
			GameState=0;
			startup();
		}
	}
}

int main()
{
	startup();  // 数据初始化	
	while (1) //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();  // 与用户输入有关的更新
	}
	GameOver();
	return 0;
}
